﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SDT.Model;

namespace SDT.Site
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = 0 }, // Parameter defaults
                new string[] { "SDT.Site.Controllers" } // Parameter defaults
            );
            
            routes.MapRoute(
                "Product",
                "Product/{action}/{id}/{priceid}/{catid}/{holidayid}/{eventid}/{sellid}",
                new { controller = "Product", action = "Index", id = 0, catid = 0, priceid = 0, holidayid = 0, eventid = 0, sellid=0 }, // Parameter defaults
                new string[] { "SDT.Site.Controllers" } // Parameter defaults
            );
            routes.MapRoute(
                "Deal",
                "deal/{action}/{id}/{priceid}/{typeid}/{holidayid}",
                new { controller = "Deal", action = "Index", id = 0, priceid = 0, holidayid = 0, typeid = 0 }, // Parameter defaults
                new string[] { "SDT.Site.Controllers" } // Parameter defaults
            );
            routes.MapRoute(
                "UrlRewrite", // Route name
                "{controller}/{action}/{id}/{title}", // URL with parameters
                new { controller = "Article", action = "Details", id = 0, title = (string)null }, // Parameter defaults
                new string[] { "SDT.Site.Controllers" } // Parameter defaults
            );
           
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
            //RouteTable.Routes.RouteExistingFiles = true;
            //RegisterRoutes(RouteTable.Routes);
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
            Application["OnlineUsers"] = 0;
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            CounterManager.Counter();            
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            Application.UnLock();
        }

        protected void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate 
            // mode is set to InProc in the Web.config file. 
            // If session mode is set to StateServer or SQLServer, 
            // the event is not raised.
            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
        }

    }
}